<?php


namespace app\admin\controller;


class Feedback extends Base
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $param=input();
        $page = intval($param['page']) < 1 ? 1 : $param['page'];
        $limit = intval($param['limit']) < 1 ? $this->_pagesize : $param['limit'];
        $offset = $limit * ($page - 1);

        $where=[];
        $total = db('feedback')->where($where)->count();
        $list = db('feedback')->where($where)->order('is_admin_read','asc')->order('time_updated','desc')->limit($offset,$limit)->select();

        foreach ($list as &$item){
            $item['time_updated_text']=date('m-d H:i:s',$item['time_updated']);
            $item['time_added_text']=date('m-d H:i:s',$item['time_added']);
        };
        $this->assign('list',$list);

        $this->assign('total', $total);
        $this->assign('page', $page);
        $this->assign('limit', $limit);
        $param = input();
        $param['page'] = '{page}';
        $param['limit'] = '{limit}';
        $this->assign('param', $param);
        return $this->fetch('admin@feedback/index');
    }

    public function detail(){
        $device_id = input("device_id");
        $app_id = input("app_id");

        $param=input();
        $page = intval($param['page']) < 1 ? 1 : $param['page'];
        $limit = intval($param['limit']) < 1 ? $this->_pagesize : $param['limit'];
        $offset = $limit * ($page - 1);

        $where=[];
        if(empty($device_id)){
            $this->error('数据有误！');
        }
        $where['device_id']=$device_id;

        $total = db('feedback_log')->where($where)->count();
        $list = db('feedback_log')->where($where)->order('time_added','desc')->limit($offset,$limit)->select();

        foreach ($list as &$item){
            $item['time_added_text']=date('m-d H:i:s',$item['time_added']);
        };
        //更新已读状态
        db('feedback_log')->where('is_reply',0)->where('device_id',$device_id)->update(['is_read'=>1]);
        db('feedback')->where('is_admin_read',0)->where('device_id',$device_id)->update(['is_admin_read'=>1]);
        $deviceInfo = db('device')->where('device_id',$device_id)->where('app_id',$app_id)->find();
        $this->assign('deviceInfo',$deviceInfo);
        $this->assign('total', $total);
        $this->assign('page', $page);
        $this->assign('limit', $limit);

        $this->assign('list',$list);
        return $this->fetch('admin@feedback/detail');
    }
    public function reply(){
        $device_id = input('device_id');
        $app_id = input('app_id');
        $find = db('feedback_log')->where('device_id', $device_id)->where('app_id', $app_id)->order('time_added','desc')->find();
        if (empty($find)) {
            $this->error('数据有误！');
        }

        if(request()->isPost()){
            $data = input();
            unset($data['id']);
            $insertArray=[
                'device_id' => $find['device_id'],
                'device_name' => $find['device_name'],
                'app_id' => $find['app_id'],
                'app_name' => $find['app_name'],
                'content' => $data['content'],
                'type' => $find['type'],
                'is_reply'=>1,
                'reply_id'=>$find['id'],
                'app_version' => $find['app_version'],
                'contact' => '',
                'time_added' => time()
            ];

//            dump($insertArray);exit;
            $id = db('feedback_log')->insertGetId($insertArray);
            if($id>0){
                $this->success('回复成功！');

            }else{
                $this->error('回复失败！');

            }

        }else {

            $this->assign('info', $find);
        }
        return $this->fetch('admin@feedback/reply');

    }

    public function dellog()
    {
        $param = input();
        $ids = $param['ids'];

        if (!empty($ids)) {
            $where = [];
            $where['id'] = ['in', $ids];
            $return = db('feedback_log')->where($where)->delete();
            if ($return !== false) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败！');
            }
        }
        return $this->error('参数错误');
    }
}